//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#include <common/identified.h>
#include "mainview.h"
#include "modeldataobjectadapter.h"
#include "objectpage.h"
#include "objectstab.h"
#include "primitives.h"
//
ObjectsTab::ObjectsTab(MainView *p_main_view,QWidget *p_parent)
:
   QTabWidget(p_parent),
   View(),
   main_view(p_main_view)
{
   setTabsClosable(true);
   //setDocumentMode(true);
   connect(this,SIGNAL(currentChanged(int)),this,SLOT(on_current_changed(int)));
   connect(this,SIGNAL(tabCloseRequested(int)),this,SLOT(on_tab_close_requested(int)));
}
//
void ObjectsTab::on_current_changed(int index)
{
   if(index>=0)
   {
      ObjectPage * const page=qobject_cast<ObjectPage*>(widget(index));
      if(page)
      {
         page->activate();
      }
   }
}
void ObjectsTab::on_tab_close_requested(int index)
{
   ObjectPage * const page=qobject_cast<ObjectPage*>(widget(index));
   if(page)
   {
      removeTab(index);
      pages.remove(page->get_object()->get_id());
      delete page;
   }
}
//
void ObjectsTab::handle_set_attr_group()
{
   for(PageHash::iterator itr=pages.begin();itr!=pages.end();++itr)
   {
      int i=indexOf(itr.value());
      Q_ASSERT(i>=0);
      removeTab(i);
      delete itr.value();
   }
   pages.clear();
}
void ObjectsTab::handle_post_primitive(const Primitive &prim)
{
   using namespace Primitives;
   switch(prim.type)
   {
   case type_Reset:
      break;
   case type_SetDataObjectName:
      {
         const SetDataObjectName& p=static_cast<const SetDataObjectName&>(prim);
         PageHash::iterator itr=pages.find(p.get_object_id());
         if(itr!=pages.end())
         {
            int i=indexOf(itr.value());
            if(i>=0)
            {
               setTabText(i,p.get_adapter().get_object_name(get_document()->lookup_object(itr.key())));
            }
         }
      }
      break;
   case type_DelDataObject:
      {
         const DelDataObject& p=static_cast<const DelDataObject&>(prim);
         if(!p.was_undo())
            remove_page(p.get_old_object_id());
      }
      break;
   case type_NewDataObject:
      {
         const NewDataObject& p=static_cast<const NewDataObject&>(prim);
         if(p.was_undo())
            remove_page(p.get_new_object_id());
      }
      break;
   }
}
//
void ObjectsTab::open_page(Identified *p_object)
{
   PageHash::iterator itr=pages.find(p_object->get_id());
   if(itr!=pages.end())
   {
      setCurrentWidget(itr.value());
      return;
   }
   ModelDataObjectAdapter * const adapt=ModelDataObjectAdapter::get_adapter(p_object->metaObject()->className());
   if(!adapt)
      return;
   ObjectPage * const new_page=adapt->create_page(main_view,this);
   pages.insert(p_object->get_id(),new_page);
   addTab(new_page,adapt->get_type_icon(),adapt->get_object_name(p_object));
   new_page->set_object(p_object);
}
void ObjectsTab::close_page(Identified *p_object)
{
   remove_page(p_object->get_id());
}
void ObjectsTab::remove_page(Id id)
{
   PageHash::iterator itr=pages.find(id);
   if(itr!=pages.end())
   {
      ObjectPage * const page=itr.value();
      int i=indexOf(page);
      Q_ASSERT(i>=0);
      removeTab(i);
      pages.remove(id);
      delete page;
   }
}
